package com.jee.ssm.modules.posUser.services;

import com.github.pagehelper.PageInfo;
import com.jee.ssm.model.Account;
import com.jee.ssm.model.PosUser;
import com.jee.ssm.model.PosuserBerth;
import com.jee.ssm.model.json.Tip;
import com.jee.ssm.modules.posUser.dao.PosUserDao;
import com.jee.ssm.modules.posuserBerth.dao.PosuserBerthDao;
import com.jee.ssm.modules.ssm.dao.AccountDao;
import com.jee.ssm.modules.ssm.services.BaseService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
* 手持机用户管理 Service
* @author GaoXiang
* @version 1.0
*/
@Service
public class PosUserService extends BaseService<PosUser> {

    @Resource
    private PosUserDao posUserDao;
    @Resource
    private AccountDao accountDao;
    @Resource
    private PosuserBerthDao posuserBerthDao;

    @Autowired
    private AmqpTemplate amqpTemplate;


    /**
     * @Description 封装需要关联删除的信息
     * @author yanzengbao
     * @date 2019/8/8 下午2:26
     */
    private void aboutDelete(String userId) throws Exception{
        //登录帐号删除
        Account account = accountDao.findByInfoId(userId);
        if(account != null){
            accountDao.deleteById(account.getId());
        }
        //用户车位关联信息删除
        List<PosuserBerth> posuserBerths = posuserBerthDao.selectByUserId(userId);
        if(posuserBerths != null && !posuserBerths.isEmpty()){
            for (PosuserBerth pb:posuserBerths) {
                posuserBerthDao.deleteById(pb.getId());
            }
        }
    }

    /**
    * 保存数据
    * @param posUser 实体对象
    * @return 实体id
    * @throws Exception 数据保存异常
    */
    public Integer insert(PosUser posUser) throws Exception {
        return posUserDao.insert(posUser);
    }

    /**
    * 根据 id 修改
    * @param posUser 带id的实体对象
    * @return 受影响的行数
    * @throws Exception 数据修改异常
    */
    public Integer updateById(PosUser posUser) throws Exception {
        return posUserDao.updateById(posUser);
    }

    /**
    * 根据 id 删除
    * @param id 数据id
    * @return 受影响的行数
    * @throws Exception 数据删除异常
    */
    public Integer deleteById(String id) throws Exception {
        //关联删除
        aboutDelete(id);
        return posUserDao.deleteById(id);
    }

    /**
    * 根据 id 查找
    * @param id 实体id
    * @return 实体
    */
    public PosUser selectById(String id) {
        return posUserDao.selectById(id);
    }

    /**
    * 根据 id 批量删除
    * @param ids 要删除的id
    * @return 影响的行数
    * @throws Exception 数据删除异常
    */
    public Integer deleteByIds(List<String> ids) throws Exception {
        if(ids != null && !ids.isEmpty()){
            for (String userId:ids) {
                aboutDelete(userId);
            }
        }
        return posUserDao.deleteByIds(ids);
    }

    /**
    * 查询列表
    * @param map 参数
    * @return 列表
    */
    public PageInfo<PosUser> list(Map map) {
        return posUserDao.list(map);
    }

    /**
    * 查询列表 带分页
    * @param map 参数
    * @param page 页码
    * @param size 每页大小
    * @return 列表
    */
    public PageInfo<PosUser> list(Map map, int page, int size) {
        return posUserDao.list(map,page,size);
    }

    public Tip send(String code, String msg){

        amqpTemplate.convertAndSend(code,msg);
        return new Tip();
    }
}
